package net.xfok.switches;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import backport.android.bluetooth.BluetoothAdapter;

/* 
 * Since 1.5 doesn't include bluetooth api,
 * so,use the hidden api,intent of updating a bluetooth state is
 * "com.android.settings.bluetooth.intent.action.EXTENDED_BLUETOOTH_STATE_CHANGED"
 * Since the emulator doesn't support Bluetooth,So this Class may not work.
 */
public class ButtonBluetooth extends TButton
{

	private final static String INTENT_ACTION = "net.xfok.switches.ButtonBluetooth";
	private Intent _intent;

	public int getButtonBmp(Context context)
	{
		if(_intent!=null)
		{
			int state = _intent.getIntExtra("android.bluetooth.adapter.extra.STATE", 0);
			return (state == 12)?R.drawable.bt_on:R.drawable.bt_off;
		}
		/* 
		 * And since the intent doesn't include an extra to indicate the newest state
		 * so get the state from Setting.Secure.
		 */
		String bluetoothString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.BLUETOOTH_ON);
		if (bluetoothString.equals("0"))
		{
//			Log.v("armNclover", "bluetooth is disabled.");
			return R.drawable.bt_off;
		} else
		{
//			Log.v("armNclover", "bluetooth mode is enabled.");
			return R.drawable.bt_on;
		}
	}

	@Override
	public void toggleSetting(Context context)
	{
		String bluetoothString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.BLUETOOTH_ON);
		BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
		if (bluetoothString.equals("0"))
			btAdapter.enable();
		else
			btAdapter.disable();
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public int canHandleIntent(Intent intent)
	{
		if (intent.getAction().equals("android.bluetooth.adapter.action.STATE_CHANGED"))
		{
			_intent = intent;
			return ToggleWidget.HANDLING_TYPE_DONE;
//		} else if(intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
//		{
//			return ToggleWidget.HANDLING_TYPE_SEQUENTIAL;
		}else
		{
			return ToggleWidget.HANDLING_TYPE_UNABLE;
		}
	}
}
